package me.luger.core.commom.util;

import org.apache.commons.lang3.StringUtils;

import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.util.Map;
import java.util.Random;

/**
 * @Title KwStringUtil.java
 * @Description 字符串处理类
 * @Copyright 2015 © kuwo.cn
 * @author TianHaoyuan
 * @Created 2015/12/15
 * @Version: 1.0
 */
public class StringUtil extends StringUtils {

	private static final String[] hex = new String[]{"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1A", "1B", "1C", "1D", "1E", "1F", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B", "3C", "3D", "3E", "3F", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D", "5E", "5F", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A", "6B", "6C", "6D", "6E", "6F", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7A", "7B", "7C", "7D", "7E", "7F", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8A", "8B", "8C", "8D", "8E", "8F", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9A", "9B", "9C", "9D", "9E", "9F", "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF", "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB", "CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF", "E0", "E1", "E2", "E3", "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF", "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF"};
	private static final byte[] val = new byte[]{63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 63, 63, 63, 63, 63, 63, 63, 10, 11, 12, 13, 14, 15, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 10, 11, 12, 13, 14, 15, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63};


	private static Random RANDGEN = new Random();

	public static enum RandomType {
		ALL(("0123456789" + "abcdefghijklmnopqrstuvwxyz"
				+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray()), LLETTER(
				("abcdefghijklmnopqrstuvwxyz").toCharArray()), LLETTER_NUM(
				("0123456789" + "abcdefghijklmnopqrstuvwxyz").toCharArray()), ULETTER(
				("ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray()), ULETTER_NUM(
				("0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray()), NUM(
				("0123456789").toCharArray());

		private char[] charArray;

		public char[] getCharArray() {
			return charArray;
		}

		private RandomType(char[] charArray) {
			this.charArray = charArray;
		}
	}

	private static final String EMPTY_MARK = "";

	public static final String randomGid(){
		return random(12, RandomType.ALL);
	}

	public static final String randomToken(){
		return random(12, RandomType.ALL);
	}

	public static final String randomSecret() {
		return random(10, RandomType.ALL);
	}

	public static final String randomAppkey() {
		return random(12, RandomType.LLETTER_NUM);
	}

	public static final String random(int length, RandomType type) {
		if (length < 1) {
			return null;
		}
		char[] randomArray = type.getCharArray();
		int randomArraySize = randomArray.length;
		char[] randBuffer = new char[length];
		for (int i = 0; i < randBuffer.length; i++) {
			randBuffer[i] = randomArray[RANDGEN.nextInt(randomArraySize)];
		}
		return new String(randBuffer);
	}

	public static String upperInitial(String word) {
		char[] chars = word.toCharArray();
		char initial = chars[0];
		if (initial >= 'a' && initial <= 'z') {
			chars[0] -= 32;
			return String.valueOf(chars);
		} else {
			return word;
		}
	}

	/**
	 * java-encodeURI 解决地址传参乱码问题
	 * 
	 * @param str
	 * @param enc
	 * @return
	 * @throws Exception
	 */
	public static String strEncode(String str, String enc) throws Exception {
		return URLEncoder.encode(str, enc);
	}

	/**
	 * jsp页面调用方法生成select option
	 * 
	 * @param optVals
	 * @param optNames
	 * @param defaultVal
	 * @param id
	 * @param className
	 * @return
	 */
	public static String getSelectStr(String[] optVals, String[] optNames,
			String defaultVal, String id, String name, String className,
			String extra) {
		if (optVals.length != optNames.length)
			return "";
		String out = new String();
		out += "<select " + extra + "id='" + id + "' name='" + name
				+ "' class='" + className + "'>\n";
		for (int i = 0; i < optVals.length; i++) {
			String option = optVals[i];
			String disName = optNames[i];
			out += "<option value='" + option + "'";
			if (option.equals(defaultVal)) {
				out += "selected='selected'";
			}
			out += ">" + disName + "</option>\n";
		}
		out += "</select>\n";
		return out;
	}

	/**
	 * jsp页面调用方法生成select option
	 * 
	 * @param map
	 * @param defaultVal
	 * @param id
	 * @param className
	 * @return
	 */
	public static String getSelectStr(Map<String, String> map,
			String defaultVal, String id, String name, String className,
			String extra) {
		if (null == map || map.isEmpty()) {
			return "";
		}
		String out = new String();
		out += "<select " + extra + "id='" + id + "' name='" + name
				+ "' class='" + className + "'>\n";
		for (Map.Entry<String, String> entry : map.entrySet()) {
			String disName = entry.getValue();
			String option = entry.getKey();
			out += "<option value='" + option + "'";
			if (option.equals(defaultVal)) {
				out += "selected='selected'";
			}
			out += ">" + disName + "</option>\n";
		}
		out += "</select>\n";
		return out;
	}

	/**
	 * @Title: String valueToString(Object value)
	 * @Description: 将参数转换成字符串
	 * @param value
	 *            转换前参数
	 * @return 转化后参数
	 */
	public static String obj2String(Object value) {
		if (value == null) {
			return EMPTY_MARK;
		}
		if (value instanceof Integer) {
			return String.valueOf(value);
		} else if (value instanceof String) {
			return (String) value;
		} else if (value instanceof Double) {
			return String.valueOf(value);
		} else if (value instanceof Float) {
			return String.valueOf(value);
		} else if (value instanceof Long) {
			return String.valueOf(value);
		} else if (value instanceof Boolean) {
			return String.valueOf(value);
		} else {
			return JsonUtil.toString(value);
		}
	}

	/**
	 * @Title: String valueToString(Object value)
	 * @Description: 将参数转换成字符串
	 * @param value
	 *            转换前参数
	 * @return 转化后参数
	 */
	public static <T> T string2Value(String value, Class<T> returnType) {
		if (value == null) {
			return null;
		}
		if (returnType == Integer.class) {
			return (T) Integer.valueOf(value);
		} else if (returnType == String.class) {
			return (T) value;
		} else if (returnType == Double.class) {
			return (T) String.valueOf(value);
		} else if (returnType == Float.class) {
			return (T) String.valueOf(value);
		} else if (returnType == Long.class) {
			return (T) String.valueOf(value);
		} else if (returnType == Boolean.class) {
			return (T) String.valueOf(value);
		} else {
			return (T) JsonUtil.parse(value);
		}
	}

	public static String value2String(Object obj){
		if(null == obj){
			return "";
		}
		return obj.toString();
	}

	/**
	 * 格式化数字
	 * 
	 * @param f
	 * @param formatStr
	 *            (#0.00,如20.666会成 20.66) (###,###.0000,如 200300.00会成
	 *            200,300.0000) (00.0000%,如0.666会成 66.6000%)
	 *            (0.000E0000,如12345678.345会成 1.235E0007)
	 * @return
	 */
	public static String twoDecimal(double f, String formatStr) {
		if (null == formatStr || "".equals(formatStr)) {
			formatStr = "#0.00";
		}
		DecimalFormat decimal = new DecimalFormat(formatStr);
		return decimal.format(f);
	}

	public static String escapeHtml(String s) {
		if(!StringUtils.isNoneBlank(s))return "";
		return s.replaceAll("'", "’").replaceAll(",", "，")
				.replaceAll("\"", "”").replaceAll("&apos;", "‘").replaceAll("&nbsp;"," ");
	}

	public static String escape(String s) {
		StringBuffer sbuf = new StringBuffer();
		int len = s.length();

		for(int i = 0; i < len; ++i) {
			char ch = s.charAt(i);
			if(65 <= ch && ch <= 90) {
				sbuf.append((char)ch);
			} else if(97 <= ch && ch <= 122) {
				sbuf.append((char)ch);
			} else if(48 <= ch && ch <= 57) {
				sbuf.append((char)ch);
			} else if(ch != 45 && ch != 95 && ch != 46 && ch != 42 && ch != 47 && ch != 43 && ch != 64) {
				if(ch <= 127) {
					sbuf.append('%');
					sbuf.append(hex[ch]);
				} else {
					sbuf.append('%');
					sbuf.append('u');
					sbuf.append(hex[ch >>> 8]);
					sbuf.append(hex[255 & ch]);
				}
			} else {
				sbuf.append((char)ch);
			}
		}

		return sbuf.toString();
	}

	public static String unescape(String s) {
		StringBuffer sbuf = new StringBuffer();
		int i = 0;

		for(int len = s.length(); i < len; ++i) {
			char ch = s.charAt(i);
			if(65 <= ch && ch <= 90) {
				sbuf.append((char)ch);
			} else if(97 <= ch && ch <= 122) {
				sbuf.append((char)ch);
			} else if(48 <= ch && ch <= 57) {
				sbuf.append((char)ch);
			} else if(ch != 45 && ch != 95 && ch != 46 && ch != 42 && ch != 43 && ch != 47 && ch != 64) {
				if(ch == 37) {
					byte cint = 0;
					int var6;
					if(117 != s.charAt(i + 1) && 85 != s.charAt(i + 1)) {
						var6 = cint << 4 | val[s.charAt(i + 1)];
						var6 = var6 << 4 | val[s.charAt(i + 2)];
						i += 2;
					} else {
						var6 = cint << 4 | val[s.charAt(i + 2)];
						var6 = var6 << 4 | val[s.charAt(i + 3)];
						var6 = var6 << 4 | val[s.charAt(i + 4)];
						var6 = var6 << 4 | val[s.charAt(i + 5)];
						i += 5;
					}

					sbuf.append((char)var6);
				}
			} else {
				sbuf.append((char)ch);
			}
		}

		return sbuf.toString();
	}

	/**
	 * 首字母转成大写
	 * @param name
	 * @return
	 */
	public static String firstChar2UpperCast(String name) {
		char[] cs=name.toCharArray();
		cs[0]-=32;
		return String.valueOf(cs);
	}

	/**
	 * 首字母转成小写
	 * @param name
	 * @return
	 */
	public static String firstChar2LowerCast(String name) {
		char[] cs=name.toCharArray();
		cs[0]+=32;
		return String.valueOf(cs);
	}

	public boolean isAllBlank(CharSequence... css){
		for(CharSequence cs : css){

		}
		return false;
	}

	public static String format(String msg,Object... array){
		return MessageFormat.format(msg, array);
	}

	public static void main(String[] args) throws Exception {
		/*
		 * String word = "?1Abbsde"; String result =
		 * KwStringUtil.upperInitial(word); System.out.println(result);
		 * 
		 * String str = strEncode("你好", "utf-8"); System.out.println(str);
		 * String aa = KwStringUtil.randomAppkey(); //String aa =
		 * KwStringUtil.random(10, KwStringUtil.RandomType.LLETTER_NUM);
		 * System.out.println("randomAppkey:" + aa);
		 * 
		 * String bb = KwStringUtil.randomSecret(); System.out.println("secret:"
		 * + bb);
		 */
		String aa = StringUtil.randomAppkey();
		System.out.println(aa);
		
		String aaa = StringUtil.randomSecret();
		System.out.println(aaa);
	}

}
